package com.shundr.shipper.cargo.http;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.shundr.shipper.cargo.model.CargoInfo;
import com.shundr.shipper.cargo.model.CargoTemplateInfo;
import com.shundr.shipper.cargo.model.MyCargoInfo;
import com.shundr.shipper.frame.constant.LCommands;
import com.shundr.shipper.frame.constant.LContext;
import com.shundr.shipper.frame.http.HttpResult;
import com.shundr.shipper.frame.http.LHttpRequest;
import com.shundr.shipper.frame.http.LHttpRequest.RequestCompleteListener;

public class CargoRequest extends LHttpRequest implements
		RequestCompleteListener {

	public static final int GET_CARGO_LIST_SUCCESSED = 1;// 获取货源列表成功
	public static final int GET_CARGO_INFO_SUCCESSED = 2;// 获取货源详情成功
	public static final int SUBMIT_CARGO_SUCCESSED = 3;// 发货成功
	public static final int GET_MY_CARGO_LIST_SUCCESSED = 4;// 获取发货列表成功
	public static final int DELETE_CARGO_SUCCESSED = 5;// 删除货源成功
	public static final int GET_CARGO_TEMPLATE_LIST_SUCCESSED = 6;// 获取发货模板列表成功
	public static final int DELETE_CARGO_TEMPLATE_SUCCESSED = 7;// 删除模板成功
	
	public static final int RESEND_CARGO_SUCCESSED = 8;// 重发货源成功
	public static final int OFF_SHELF_CARGO_SUCCESSED = 9;// 下架货源成功

	public static final int GET_CARGO_LIST_FAILED = -1;// 获取货源列表失败
	public static final int GET_CARGO_INFO_FAILED = -2;// 获取货源详情失败

	private Handler mHandler;
	private Message mMessage;
	private Context mContext;

	private Map<String, Object> param;
	private String command;

	public CargoRequest(Context context, Handler handler) {
		this.mContext = context;
		this.mHandler = handler;
		this.mMessage = mHandler.obtainMessage();
		this.setRequestCompleteListener(this);
	}

	/**
	 * 
	 * getCargoLsit(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param cargoCategory
	 *            货物类型,名称
	 * @param cargoTruckType
	 *            货车类型
	 * @param cargoTruckLength
	 *            货车长度
	 * @param cargoAmount
	 *            货物数量
	 * @param srcPlaceCode
	 *            起始地址代码
	 * @param dstPlaceCode
	 *            目标 地址 代码
	 * @param startSize
	 *            记录起始位置
	 * @param size
	 *            记录条数
	 * @exception
	 * @since 1.0.0
	 */
	public void getCargoLsit(String cargoCategory, String cargoTruckType,
			String cargoTruckLength, String cargoAmount, String srcPlaceCode,
			String dstPlaceCode, int startSize, int size) {
		if (startSize == 0) {
//			getSearchCargoListNum(cargoCategory, cargoTruckType, cargoTruckLength, cargoAmount, srcPlaceCode, dstPlaceCode);
		}
		param = new HashMap<String, Object>();
		param.put("cargoCategory", cargoCategory);
		param.put("cargoTruckTypeCode", cargoTruckType);
		param.put("cargoTruckLength", cargoTruckLength);
		param.put("cargoAmount", cargoAmount);
		param.put("srcPlaceCode", srcPlaceCode);
		param.put("dstPlaceCode", dstPlaceCode);
		param.put("startSize", startSize);//
		param.put("size", size);
		command = LCommands.GET_CARGO_LIST;
		requestByGet(param);
	}
	
	/**
	 * 查询货源总数量，因为查询货源的时候已经可以返回总数量，这里只是单纯记录查询操作，可以不用处理返回
	 * getSearchCargoListNum(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param cargoCategory
	 * @param cargoTruckType
	 * @param cargoTruckLength
	 * @param cargoAmount
	 * @param srcPlaceCode
	 * @param dstPlaceCode
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void getSearchCargoListNum(String cargoCategory, String cargoTruckType,
			String cargoTruckLength, String cargoAmount, String srcPlaceCode,
			String dstPlaceCode) {
		param = new HashMap<String, Object>();
		param.put("cargoCategory", cargoCategory);
		param.put("cargoTruckTypeCode", cargoTruckType);
		param.put("cargoTruckLength", cargoTruckLength);
		param.put("cargoAmount", cargoAmount);
		param.put("srcPlaceCode", srcPlaceCode);
		param.put("dstPlaceCode", dstPlaceCode);
		command = LCommands.GET_CARGO_LIST_NUM;
		requestByGet(param);
	}

	/**
	 * 货源详情
	 * 
	 * @param cargoId
	 */
	public void getCargoInfo(String cargoId) {
		param = new HashMap<String, Object>();
		String userid = "";
		if (LContext.userInfo != null)
			userid = LContext.userInfo.getUserId() + "";
		param.put("userId", userid);
		param.put("cargoId", cargoId);
		command = LCommands.GET_CARGO_INFO;
		requestByGet(param);
	}

	/**
	 * 提交货源 submitCargo(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param json
	 * @param time
	 * @param name
	 * @param mobile
	 * @param phone
	 * @param md5Id
	 * @param ttag
	 *            void
	 * @exception
	 * @since 1.0.0
	 */
	public void submitCargo(String json, String time, String name,
			String mobile, String phone,String resend) {
		param = new HashMap<String, Object>();
		param.put("cargoJsonList", json);
		param.put("cargoExpiryTime", time);
		param.put("cargoCompanyName", name);
		param.put("cargoCompanyPhone1", mobile);
		param.put("cargoCompanyPhone2", phone);
		param.put("resendTimes", resend);
		command = LCommands.SUBMIT_CARGO;
		requestByPost(param);
	}
	
	/**
	 * 获取我的货源列表
	 * getMyCargoList(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param status
	 * @param srcPlaceCode
	 * @param dstPlaceCode
	 * @param startSize
	 * @param size
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void getMyCargoList(int startSize, int size, String status,
			String srcPlaceCode, String dstPlaceCode) {
		param = new HashMap<String, Object>();
		String userId = "";
		if (LContext.userInfo != null)
			userId = LContext.userInfo.getUserId() + "";
		param.put("userId", userId);
		param.put("status", status);
		param.put("srcPlaceCode", srcPlaceCode);
		param.put("dstPlaceCode", dstPlaceCode);
		param.put("startSize", startSize);
		param.put("size", size);
		command = LCommands.GET_MY_CARGO_LIST;
		requestByGet(param);
	}
	
	/**
	 * 发货模板
	 * getCargoTemplateList(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param startSize
	 * @param size
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void getCargoTemplateList(int startSize, int size) {
		param = new HashMap<String, Object>();
		String userId = "";
		if (LContext.userInfo != null)
			userId = LContext.userInfo.getUserId() + "";
		param.put("userId", userId);
		param.put("startSize", startSize);
		param.put("size", size);
		command = LCommands.GET_CARGO_TEMPLATE_LIST;
		requestByGet(param);
	}
	
	/**
	 * 删除货源
	 * deleteMyCargo(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param cargoId
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void deleteMyCargo(int cargoId) {
		param = new HashMap<String, Object>();
		param.put("cargoId", cargoId);
		command = LCommands.DELETE_MY_CARGO;
		requestByGet(param);
	}
	
	/**
	 * 下架货源
	 * offShelfCargo(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param cargoId
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void offShelfCargo(int cargoId) {
		param = new HashMap<String, Object>();
		param.put("cargoId", cargoId);
		command = LCommands.OFF_SHELF_CARGO;
		requestByGet(param);
	}
	
	/**
	 * 重发货源
	 * resendCargo(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param cargoId
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void resendCargo(int cargoId) {
		param = new HashMap<String, Object>();
		param.put("cargoId", cargoId);
		command = LCommands.RESEND_MY_CARGO;
		requestByPost(param);
	}
	
	/**
	 * 删除常发货模板
	 * deleteCargoTemplate(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param md5Id
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void deleteCargoTemplate(String md5Id) {
		param = new HashMap<String, Object>();
		param.put("templateId", md5Id);
		command = LCommands.DELETE_CARGO_TEMPLATE;
		requestByGet(param);
	}

	@Override
	protected void initParams() {
		super.initParams();
		mParam = param;
		mCommand = command;
		mBaseContext = mContext;
	}

	@Override
	public void requestSuccessed(HttpResult result) {
		if (mHandler == null)
			return;
		if (command.equals(LCommands.GET_CARGO_LIST)) {
			mMessage.what = GET_CARGO_LIST_SUCCESSED;
			List<CargoInfo> infos = CargoParser.parserCargoList(result.getResult());
			mMessage.obj = infos;
		} else if (command.equals(LCommands.GET_CARGO_INFO)) {
			mMessage.what = GET_CARGO_INFO_SUCCESSED;
			CargoInfo infos = CargoParser.parserCargoInfo(result.getResult());
			mMessage.obj = infos;
		}else if (command.equals(LCommands.SUBMIT_CARGO)) {
			mMessage.what = SUBMIT_CARGO_SUCCESSED;
			mMessage.obj = result.getDescription();
		}else if (command.equals(LCommands.GET_MY_CARGO_LIST)) {
			mMessage.what = GET_MY_CARGO_LIST_SUCCESSED;
			List<MyCargoInfo> infos = CargoParser.parserMyCargoList(result.getResult());
			mMessage.obj = infos;
		}else if (command.equals(LCommands.DELETE_MY_CARGO)) {
			mMessage.what = DELETE_CARGO_SUCCESSED;
			mMessage.obj = result.getDescription();
		}else if (command.equals(LCommands.GET_CARGO_TEMPLATE_LIST)) {
			mMessage.what = GET_CARGO_TEMPLATE_LIST_SUCCESSED;
			List<CargoTemplateInfo> infos = CargoParser.parserCargoTemplateList(result.getResult());
			mMessage.obj = infos;
		}else if (command.equals(LCommands.DELETE_CARGO_TEMPLATE)) {
			mMessage.what = DELETE_CARGO_TEMPLATE_SUCCESSED;
			mMessage.obj = result.getDescription();
		}else if (command.equals(LCommands.RESEND_MY_CARGO)) {
			mMessage.what = RESEND_CARGO_SUCCESSED;
			mMessage.obj = result.getDescription();
		}else if (command.equals(LCommands.OFF_SHELF_CARGO)) {
			mMessage.what = OFF_SHELF_CARGO_SUCCESSED;
			mMessage.obj = result.getDescription();
		}
		mHandler.sendMessage(mMessage);
	}

	@Override
	public void requestFailed(HttpResult result) {
		if (mHandler == null)
			return;
		// 请求失败,result的返回码可判断失败原因，进行如下操作
		mMessage.what = REQUEST_FAILED;
		mMessage.obj = result.getDescription();
		mHandler.sendMessage(mMessage);
	}

}
